package com.mobilezu.mobile.pushserver.util;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import com.mobilezu.mobile.pushserver.console.vo.SessionVO;
import com.mobilezu.mobile.pushserver.model.PushUser;
import com.mobilezu.mobile.pushserver.service.PushUserService;
import com.mobilezu.mobile.pushserver.service.ServiceManager;
import com.mobilezu.mobile.pushserver.xmpp.session.ClientSession;
import com.mobilezu.mobile.pushserver.xmpp.session.SessionManager;

public class Constants {

	public static final String client_version = "0.6.3";
	public static final String server_host = "192.168.1.245";//��������IP

	// ��ݿ����Ӳ���
	public static final String sqlserver_host = "127.0.0.1";//��ݿ��IP
	public static final int sqlserver_post = 3306;//��ݿ�Ķ˿ں�
	public static final String sqlserver_user = "root";//��ݿ���û���
	public static final String sqlserver_passwd = "";//��ݿ������
	public static final String sqlserver_db_name = "push_server_db";//��ݿ����

	public static final String web_addr_for_web_show_image = "http://"
			+ server_host + ":8080/XiaoDaoPushServer/MyServlet/";
	public static final String server_name_version = "XiaoDaoPushServer_v"
			+ client_version;
	public static final String client_name_version = "XiaoDaoPushClient_v"
			+ client_version;
	public static final String client_download_url = "http://192.168.1.245:8080/XiaoDaoPushServer/"
			+ client_name_version + ".apk";

	// ��Ϣ����ԴTool
	public static final String TOOL_WEB_SPECIFIED = "WEB_SPECIFIED";
	public static final String TOOL_CLIENT_SPECIFIED = "CLIENT_SPECIFIED";
	public static final String TOOL_WEB_ALL = "WEB_ALL";
	public static final String TOOL_WEB_ONLINE = "WEB_ONLINE";

	// ��Ϣ״̬Status
	public static final String STATUS_NOT_YET_SEND = "NOT_YET_SEND";
	public static final String STATUS_ALREADY_SEND = "ALREADY_SEND";
	public static final String STATUS_ALREADY_RECEIVE = "ALREADY_RECEIVE";

	/**
	 * ��������Ϣ��ID
	 * 
	 * @return
	 */
	public static String getDataId() {
		Random random = new Random();
		String dataId = Integer.toHexString(random.nextInt());
		return dataId;
	}

	/**
	 * ȡ�õ�ǰ�������û�
	 * 
	 * @return
	 * @throws Exception
	 */
	public static List<SessionVO> getSessionUserToList() throws Exception {
		ClientSession[] sessions = new ClientSession[0];
		sessions = SessionManager.getInstance().getSessions().toArray(sessions);
		PushUserService pushUserService = ServiceManager.getPushUserService();
		List<SessionVO> voList = new ArrayList<SessionVO>();
		String username;
		String name;
		PushUser pushUser;
		for (ClientSession sess : sessions) {
			username = sess.getUsername();
			SessionVO vo = new SessionVO();
			vo.setUsername(username);
			pushUser = pushUserService.getPushUserByUsername(username);
			name = pushUser.getName();
			if (pushUser != null && name != null && !name.equals("")) {
				vo.setName(name);
			} else {
				vo.setName(username);
			}
			voList.add(vo);
		}
		return voList;
	}

	/**
	 * ��ȡ��ǰ����ʱ��
	 * 
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("finally")
	public static String getNowBeiJingTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		URL url;
		String timeStr = "";
		Date date;
		try {
			url = new URL("http://www.bjtime.cn");
			URLConnection uc = url.openConnection();
			uc.connect();
			long ld = uc.getDate(); // ȡ����վ����ʱ��
			date = new Date(ld); // ת��Ϊ��׼ʱ�����
			timeStr = formatter.format(date);
		} catch (IOException e) {
			date = new Date(System.currentTimeMillis());
			timeStr = formatter.format(date);
		} finally {
			return timeStr;
		}
	}

	/**
	 * ������Ϣ�ĳ��ȿ���
	 * 
	 * @return
	 */
	public static String subString(String oldStr, int endIndex) {
		String newStr = oldStr;
		if (oldStr.length() > endIndex) {
			newStr = oldStr.substring(0, endIndex) + "...";
		}
		return newStr;
	}

	/**
	 * ���˵��ظ����ַ�
	 * 
	 * @param oldStrArray
	 * @return
	 */
	public static String[] delRepeatStr(String[] oldStrArray) {
		List<String> list = Arrays.asList(oldStrArray);
		Set<String> set = new HashSet<String>(list);
		String[] newStrArray = new String[set.size()];
		Iterator<String> it = set.iterator();
		for (int i = 0; it.hasNext(); i++) {
			newStrArray[i] = (String) it.next();
		}
		return newStrArray;
	}
}
